<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品编号" prop="goodCode">
      <el-input v-model="dataForm.goodCode" placeholder="商品编号"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="goodName">
      <el-input v-model="dataForm.goodName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="展示图片" prop="showImg">
      <el-input v-model="dataForm.showImg" placeholder="展示图片"></el-input>
    </el-form-item>
    <el-form-item label="价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="价格"></el-input>
    </el-form-item>
    <el-form-item label="商品简介" prop="goodDesc">
      <el-input v-model="dataForm.goodDesc" placeholder="商品简介"></el-input>
    </el-form-item>
    <el-form-item label="售后服务" prop="saleServe">
      <el-input v-model="dataForm.saleServe" placeholder="售后服务"></el-input>
    </el-form-item>
    <el-form-item label="库存" prop="stock">
      <el-input v-model="dataForm.stock" placeholder="库存"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="startTime">
      <el-input v-model="dataForm.startTime" placeholder="开始时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="分类id" prop="cateId">
      <el-input v-model="dataForm.cateId" placeholder="分类id"></el-input>
    </el-form-item>
    <el-form-item label="上架状态 0：上架  1：未上架" prop="status">
      <el-input v-model="dataForm.status" placeholder="上架状态 0：上架  1：未上架"></el-input>
    </el-form-item>
    <el-form-item label="逻辑删除 0：删除 1：未删除" prop="del">
      <el-input v-model="dataForm.del" placeholder="逻辑删除 0：删除 1：未删除"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          goodCode: '',
          goodName: '',
          showImg: '',
          price: '',
          goodDesc: '',
          saleServe: '',
          stock: '',
          startTime: '',
          updateTime: '',
          cateId: '',
          status: '',
          del: ''
        },
        dataRule: {
          goodCode: [
            { required: true, message: '商品编号不能为空', trigger: 'blur' }
          ],
          goodName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          showImg: [
            { required: true, message: '展示图片不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '价格不能为空', trigger: 'blur' }
          ],
          goodDesc: [
            { required: true, message: '商品简介不能为空', trigger: 'blur' }
          ],
          saleServe: [
            { required: true, message: '售后服务不能为空', trigger: 'blur' }
          ],
          stock: [
            { required: true, message: '库存不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          cateId: [
            { required: true, message: '分类id不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '上架状态 0：上架  1：未上架不能为空', trigger: 'blur' }
          ],
          del: [
            { required: true, message: '逻辑删除 0：删除 1：未删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/product/good/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.goodCode = data.good.goodCode
                this.dataForm.goodName = data.good.goodName
                this.dataForm.showImg = data.good.showImg
                this.dataForm.price = data.good.price
                this.dataForm.goodDesc = data.good.goodDesc
                this.dataForm.saleServe = data.good.saleServe
                this.dataForm.stock = data.good.stock
                this.dataForm.startTime = data.good.startTime
                this.dataForm.updateTime = data.good.updateTime
                this.dataForm.cateId = data.good.cateId
                this.dataForm.status = data.good.status
                this.dataForm.del = data.good.del
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/product/good/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'goodCode': this.dataForm.goodCode,
                'goodName': this.dataForm.goodName,
                'showImg': this.dataForm.showImg,
                'price': this.dataForm.price,
                'goodDesc': this.dataForm.goodDesc,
                'saleServe': this.dataForm.saleServe,
                'stock': this.dataForm.stock,
                'startTime': this.dataForm.startTime,
                'updateTime': this.dataForm.updateTime,
                'cateId': this.dataForm.cateId,
                'status': this.dataForm.status,
                'del': this.dataForm.del
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
